@extends('layouts.common_no_menu')

@section('title')商品编辑@endsection
@section('css')
    <style>
        .info-form td {margin-right: 10px;}
        .input_width {width: 90px;}
    </style>
@endsection
@section('content')
    <body class="easyui-layout" style="">
        <div data-options="region:'north'" style="padding:5px;">
            <form id="info-form" method="post">
                <table>
                    <tr>
                        <td align="left">客户ID:<input type="text" name="customer_id" class="easyui-textbox input_width" value="@if(!empty($info)){{$info['customer_id']}}@endif" /></td>
                        <td align="left">客户名称:<input type="text" name="customer_name" class="easyui-textbox input_width" value="@if(!empty($info)){{$info['customer_name']}}@endif" /></td>
                        <td align="left">客户手机:<input type="text" name="customer_mobile" class="easyui-textbox input_width" value="@if(!empty($info)){{$info['customer_mobile']}}@endif" /></td>
                        <td align="left">配送方式:<input class="easyui-combobox" type="text" id="shipping_methods" name="shipping_methods" style="width:60px;" value="@if(!empty($info)){{$info['shipping_methods']}}@endif"/></td>

                        <td align="left" class="warehouse">仓库:<input class="easyui-combobox input_width" type="text" id="" name="warehouse_id" value="@if(!empty($info)){{$info['warehouse_id']}}@endif"/></td>
                        <td align="left" class="shipping_address">配送地址:<input type="text" name="shipping_address" class="easyui-textbox" style="width:140px;" value="@if(!empty($info)){{$info['shipping_address']}}@endif" /></td>


                        <td><a href="#" class="easyui-linkbutton" iconCls="icon-save" onclick="saveReq()" plain="true">保存</a></td>
                        <div style="clear: both"></div>
                    </tr>
                    {{--<tr>--}}
                        {{--<td align="left">描述:<input class="easyui-textbox" id="description" value="" style="width:350px;height:70px"></td>--}}
                    {{--</tr>--}}
                </table>
            </form>
        </div>
        <div data-options="region:'center',split:true" style="height: 400px;">
            <div id="wu-toolbar">
                <div class="wu-toolbar-button">
                    <a href="#" class="easyui-linkbutton" iconCls="icon-add" onclick="reqAdd()" plain="true">添加</a>
                    <a href="#" class="easyui-linkbutton" iconCls="icon-edit" onclick="goodsEdit()" plain="true">修改</a>
                    <a href="#" class="easyui-linkbutton" iconCls="icon-remove" onclick="remove()" plain="true">删除</a>
                    <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="cancel()" plain="true">取消</a>
                    <a href="#" class="easyui-linkbutton" iconCls="icon-reload" onclick="reload()" plain="true">刷新</a>
                </div>
            </div>
            <div>
                <div id="goodsList">
                </div>
                <div id="form_dialog"></div>
                <input type="hidden" id="req_id" name='req_id' value="@if(!empty($info)){{$info['id']}}@endif">
            </div>
        </div>

    </body>

@endsection
@section('js')
    <script>


        $("input[name='warehouse_id']").combobox({
            url: '/storage/warehouse/slist',
            method: 'get',
            valueField: 'id',
            textField: 'name',
            panelMaxHeight:90
        });

        $('#shipping_methods').combobox({
            panelHeight:48,
            valueField: 'id',
            textField: 'value',
            data:[{id:'1', value: "自提"},
                {id:'2', value: "配送"}],
            onSelect:function (raw) {
                if (raw.id === "1") {
                    $('.warehouse').show();
                    $('.shipping_address').hide();
                }
                if (raw.id === "2") {
                    $('.warehouse').hide();
                    $('.shipping_address').show();

                }
            }
        });

        $('#description').textbox({
            multiline:'true'
        });

        $('#goodsList').datagrid({
            url: '/sale/requisition/goodslist',
            rownumbers: true,
//            pagination: true,
//            height: 500,
//            title: '认购单详情',
            toolbar: '#wu-toolbar',
//            fit: true,
            singleSelect:"true",
            queryParams:{
                _token : $('meta[name="csrf-token"]').attr('content'),
                req_id:$("input[name='req_id']").val()
            },

            columns:[[
                { field:'id',checkbox:true},
                { field:'goods_name',title:'商品名称',width:80,align:'center'},
                { field:'goods_price',title:'商品价格',width:100,align:'center'},
                { field:'goods_num',title:'商品数量',width:150,align:'center'},
            ]]
        });

        function reqAdd(){
            var count = 0;
            var req_id = $("input[name='req_id']").val();
            $("#info-form").find('input').each(function() {
                if ($(this).val()===''&& req_id==='') {
                    count++;
                    $.messager.alert("提示信息","不能有空项提示需要点击保存!");
                    $(this).focus();
                    return false;
                }
            });
            console.log($('#info-form').serialize());
        }

        function saveReq() {
            var req_id = $("input[name='req_id']").val();
            $.ajax({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                url:'/sale/requisition/savereq',
                type:'post',
                data:$('#info-form').serialize()+'&req_id='+req_id,
                dataType:'json',
                success:function (data) {
                    if(data.code === 200){
                        $("input[name='req_id']").val(data.id);
                        $.messager.alert('提示','保存成功！');
                    }else{
                        $.messager.alert("警告", data.data , "error");
                        return false;
                    }
                }
            });
        }

        function goodsAdd() {

            $('#form_dialog').dialog({
                title: '添加信息',
                width: 500,
                height: 200,
                closed: false,
                cache: false,
                href: '/sale/requisition/goodsadd',
                modal: true,
                buttons: [{
                    text: '确定',
                    iconCls: 'icon-ok',
                    handler: save
                }, {
                    text: '取消',
                    iconCls: 'icon-cancel',
                    handler: function () {
                        $('#form_dialog').dialog('close');
                    }
                }]
            });
        }

        function goodsEdit(){

            $('#form_dialog').dialog({
                title: '添加信息',
                width: 500,
                height: 200,
                closed: false,
                cache: false,
                href: '/sale/requisition/goodsadd',
                modal: true,
                buttons: [{
                    text: '确定',
                    iconCls: 'icon-ok',
                    handler: save
                }, {
                    text: '取消',
                    iconCls: 'icon-cancel',
                    handler: function () {
                        $('#form_dialog').dialog('close');
                    }
                }]
            });
        }

        function save(){
            var req_id = $("input[name='req_id']").val();
            console.log($('#goods-form').serialize()+'&req_id='+req_id);
            $.ajax({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                url:'/sale/requisition/savegoods',
                type:'post',
                data:$('#goods-form').serialize()+'&req_id='+req_id,
                dataType:'json',
                success:function (data) {
                    if(data.code === 200){
                        $('#goodsList').datagrid('reload',{_token : $('meta[name="csrf-token"]').attr('content'), req_id:$("input[name='req_id']").val()})
                        $('#form_dialog').dialog('close');
                    }else{
                        $.messager.alert("提示信息", data.data , "error");
                    }
                }
            });
        }
    </script>
@endsection


